package com.sinodata.bsm.cicp.collector.temperature;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class TemperatureAndHumidityCollector extends Collector {
    private static Logger logger = Logger.getLogger(TemperatureAndHumidityCollector.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> result = new ArrayList<ResPropertyValue>();
        String[] propMetas = this.getPropMeta();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader br = null;
        String sensorNumber = this.getParamMap().get("设备号");
        String val1 = "";//存放温度
        String val2 = "";//存放湿度
        try {
            br = CollectorUtil.getReader("getsensor", ip);
            String readerLine = br.readLine();
            while (readerLine != null) {
                String line[] = readerLine.split("\t\t");
                if (line[0].equals(sensorNumber)) {
                    val1 = line[1];
                    val2 = line[2];
                    break;
                }
                readerLine = br.readLine();
            }

        } catch (IOException IO) {
            logger.error("failed to read file ", IO);
            throw new CollectorException(IO);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (Exception e) {
                logger.error("IOException occur ", e);
                throw new CollectorException(e);
            }
        }
        propValues[0] = ResPropertyValue.createPropValue(val1, propMetas[0]);
        propValues[1] = ResPropertyValue.createPropValue(val2, propMetas[1]);
        propValues[2] = ResPropertyValue.createPropValue(getRemoteCurrentTimeMillis(ip) + "", propMetas[2]);
        ResPropertyValue value = new ResPropertyValue();
        value.setValues(propValues);
        result.add(value);
        return result;
    }

    private long getRemoteCurrentTimeMillis(String ip) throws CollectorException {
        BufferedReader reader = null;
        try {
            String[] strDate = null;
            String[] sigDate = new String[3];
            String[] strTime = new String[2];
            reader = com.sinodata.bsm.cicp.util.CollectorUtil.getReader("date /T", ip, 300);
            String readLine = reader.readLine();
            if (readLine != null) {
                strDate = readLine.split(" ");
                sigDate = strDate[0].split("-");
            }
            reader = com.sinodata.bsm.cicp.util.CollectorUtil.getReader("time /T", ip, 300);
            readLine = reader.readLine();
            if (readLine != null) {
                strTime = readLine.trim().split(":");
                for (int i = 0; i < 2; i++) {
                    if (strTime[i].length() != 2)
                        strTime[i] = "0" + strTime[i];
                }
            }
            StringBuffer tmp = new StringBuffer().append(sigDate[0]).append(sigDate[1]).append(sigDate[2]).append(strTime[0]).append(strTime[1]).append("00");
            return getDateMillis(tmp.toString());
        } catch (Exception e) {
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                logger.error("IOException ", ex);
                throw new CollectorException(ex);
            }
        }
        return System.currentTimeMillis();
    }

    private long getDateMillis(String date) {
        Calendar calendar = Calendar.getInstance();
        int i = Integer.parseInt(date.substring(0, 4));
        calendar.set(Calendar.YEAR, i);
        i = Integer.parseInt(date.substring(4, 6));
        calendar.set(Calendar.MONTH, i - 1);
        i = Integer.parseInt(date.substring(6, 8));
        calendar.set(Calendar.DAY_OF_MONTH, i);
        i = Integer.parseInt(date.substring(8, 10));
        calendar.set(Calendar.HOUR_OF_DAY, i);
        i = Integer.parseInt(date.substring(10, 12));
        calendar.set(Calendar.MINUTE, i);
        i = Integer.parseInt(date.substring(12, 14));
        calendar.set(Calendar.SECOND, i); //设置完年／月／日／小时／分钟／秒
        return calendar.getTimeInMillis(); //转换为毫秒
    }

    public String[] getParamMeta() {
        return new String[] { "设备号" };
    }

    public String[] getPropMeta() {
        return new String[] { "温度", "湿度", "采集时间" };
    }
}
